package com.wdb007.venus.svc.biz.account;

import java.util.Date;
import java.util.UUID;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.wdb007.venus.svc.biz.manager.RabbitManager;
import com.wdb007.venus.svc.dal.bs.dao.BsAlarmDAO;
import com.wdb007.venus.svc.dal.bs.model.BsAlarmDO;
import com.wdb007.venus.svc.facade.BsAlarmService;
import com.wdb007.venus.svc.facade.bs.model.BsAlarmDTO;
import com.wdb007.venus.svc.facade.constant.BizConstants;
import com.wdb007.venus.svc.facade.exception.BizException;

@Service("bsAlarmService")
public class BsAlarmServiceImpl implements BsAlarmService {

	private Logger logger = LoggerFactory.getLogger(BsAlarmServiceImpl.class);
	
	@Autowired
	private BsAlarmDAO bsAlarmDAO;
	
	@Autowired
	private RabbitManager rabbitManager;
	
	@Override
	public void saveBsAlarm(BsAlarmDTO bsAlarm) throws BizException {
		
		BsAlarmDO q1 = new BsAlarmDO();
		q1.setAlarmType(bsAlarm.getAlarmType());
		q1.setBookgridCode(bsAlarm.getBookgridCode());
		q1.setAlarmStatus(BizConstants.BS_ALARM_STATUS_0);
		BsAlarmDO result1 = bsAlarmDAO.getByCondition(q1);
		q1.setAlarmStatus(BizConstants.BS_ALARM_STATUS_1);
		BsAlarmDO result2 = bsAlarmDAO.getByCondition(q1);
		if(result1 == null && result2 == null) {
			//存储数据
			logger.info("saveBsAlarm: {}", JSON.toJSONString(bsAlarm));
			BsAlarmDO bsAlarmDO = new BsAlarmDO();
			BeanUtils.copyProperties(bsAlarm, bsAlarmDO);
			
			bsAlarmDO.setAlarmStatus(BizConstants.BS_ALARM_STATUS_0);
			bsAlarmDO.setBookcaseId(bsAlarmDO.getBookgridCode().substring(0, 10));
			bsAlarmDO.setId(UUID.randomUUID().toString().replaceAll("-", ""));
			bsAlarmDO.setCreateTime(new Date());
			bsAlarmDO.setAlarmTime(new Date());
			bsAlarmDAO.insert(bsAlarmDO);	//保存数据
			rabbitManager.sendAlarm(JSON.toJSONString(bsAlarmDO));
		}
	}

}
